package com.icexxx.icemock;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;

import javax.sql.DataSource;

import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import cn.hutool.setting.Setting;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        String tableName = "sys_user";
        DataSource dataSource = null;
        boolean flag = false;
        File currentFile = new File(".");
        File[] listFiles = currentFile.listFiles();
        if(listFiles != null && listFiles.length != 0) {
            for (File file : listFiles) {
                if(file != null) {
                    if(file.getName().endsWith(".jar")) {
                        flag = true;
                    }
                }
            }
        }
        if(flag) {
            File parentFile = new File(currentFile, "db.setting");
            Setting setting = new Setting(parentFile, StandardCharsets.UTF_8, false);
            DSFactory factory = DSFactory.create(setting);
            dataSource = factory.getDataSource();
        }else {
            dataSource = DSFactory.get();
        }
        StringBuilder sb = new StringBuilder();
        Table tableMeta = MetaUtil.getTableMeta(dataSource, tableName);
//        String comment = tableMeta.getComment();
        sb.append("==========" + tableName + "====*10==");
        System.out.println(sb.toString());
        Collection<Column> columns = tableMeta.getColumns();
        Iterator<Column> iterator = columns.iterator();
        while (iterator.hasNext()) {
            Column column = iterator.next();
            String name = column.getName();
            String typeName = column.getTypeName();
            if ("varchar".equalsIgnoreCase(typeName)) {
                System.out.println(name + ": @str{3,5}");
            } else if ("datetime".equalsIgnoreCase(tableName)) {
                System.out.println(name + ": @date[-365,0]");
            } else {
                System.out.println(name + ": @int{3,5}");
            }
        }
    }
}
